package base.http.interceptor;


import com.blankj.utilcode.util.SPUtils;
import com.tange.ai.iot.client.gnssa.App;
import com.tange.ai.iot.client.gnssa.token.InternalAccountLoginActivity;
import com.tange.base.toolkit.PreferenceUtil;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by Administrator_LZH on 2016/6/23.
 */
public class HeaderInterceptor implements Interceptor {
    private HashMap<String, String> params;

    public HeaderInterceptor(HashMap params) {
        this.params = params;
    }

    @Override
    public Response intercept(Chain chain) throws IOException {
        Map<String, Object> refreshParamsMap = new HashMap<>();
        refreshParamsMap.put("token",  SPUtils.getInstance().getString(InternalAccountLoginActivity.PRE_TOKEN));
        Request original = chain.request();
        Request.Builder builder = original.newBuilder();
        if (params != null && params.size() > 0) {
            refreshParamsMap.putAll(params);
        }
        for (Map.Entry<String, Object> entry : refreshParamsMap.entrySet()) {
            builder.removeHeader(entry.getKey());
            builder.addHeader(entry.getKey(), String.valueOf(entry.getValue()));
        }
        builder.method(original.method(), original.body());
        return chain.proceed(builder.build());
    }

}
